package com.lyn.module_custom_network;

import android.text.TextUtils;

import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * created by lyn at 2021/10/14
 */
public class LynSocketClientHelper {
    private final static class LynSocketClientHelperTypeClass {
        private final static LynSocketClientHelper instance = new LynSocketClientHelper();
    }

    public static LynSocketClientHelper getInstance() {
        return LynSocketClientHelperTypeClass.instance;
    }


    private LynWebSocketClient client;

    public LynWebSocketClient getClient() {
        return client;
    }

    public void connection(String url) throws Exception {
        if (client != null) return;
        if (TextUtils.isEmpty(url)) return;
        URI uri = URI.create(url);
        client = new LynWebSocketClient(uri);
        synchronized (client) {
            client.connectBlocking();
        }
    }

    public void closeConnection() throws Exception{
        if (client == null) return;
        synchronized (client) {
            client.close();
            client = null;
        }
    }

    public void sendMessage(String message) throws Exception{
        if (TextUtils.isEmpty(message)) return;
        if (client == null) return;
        synchronized (client) {
            client.send(message);
        }
    }
}
